
package com.nc.delivery_project.server.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;





import com.nc.delivery_project.server.dao.AbstractDao;
import com.nc.delivery_project.server.dao.DepartmentDao;
import com.nc.delivery_project.server.dao.UserDao;
import com.nc.delivery_project.server.db_util.ConnectionManager;
import com.nc.delivery_project.server.domain.City;
import com.nc.delivery_project.server.domain.Department;
import com.nc.delivery_project.server.util.CRUD;


public class DepartmentService extends  BaseService<Department> {
	
	

	public DepartmentService() {
		this(new DepartmentDao());
	}
	
	public DepartmentService(AbstractDao<Department> deptDao) {
		super(deptDao);
	}
	
	public DepartmentService(Connection connection, boolean isAutoCloseConnection) {
	        super(new DepartmentDao(), connection, isAutoCloseConnection);
	}
	@Override
	public Department read(int id) {
		Department dept = null;
		
		try {
			Connection conn = getConnection();
			dao.setConnection(conn);
			dept = dao.read(id);
			
			// достаём по id города  его поля
			if (dept !=null) {
				City deptCity = new CityService(conn, false).read(dept.getCity().getCityId());
				if (deptCity != null) {
					dept.setCity(deptCity);
					dept.getCity().setCityTitle(deptCity.getCityTitle());
					dept.getCity().setStatePr(deptCity.getStatePr());	
					
				}
					
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
		}
						
		return dept;
	}

	

	@Override
	public List<Department> read() {
		List<Department> depts = null;
		
		try {
			Connection conn = getConnection();
	        dao.setConnection(conn);
			depts = dao.read();
			
			
			CityService cityService = new CityService(conn, false);
			
			for (Department dept : depts) {
				dept.setCity(cityService.read(dept.getCity().getCityId()));

					
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
		
		
		return depts;
	}

	
	
	

	public List<Department> loadDeptByCityId(int cityId) throws SQLException {
	
		
		List<Department> depts = null;
		
		try {
			Connection conn = getConnection();
			dao.setConnection(conn);
			depts = ((DepartmentDao)dao).readByCityId(cityId);
			
			CityService cityService = new CityService(conn, false);
			
			for (Department dept : depts) {
				dept.setCity(cityService.read(dept.getCity().getCityId()));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
		
		
		return depts;
	}

	

}
